#!/usr/bin/env python
import sys
import pygtk, gtk
import gtksourceview2
import gobject
import cairo, pango

class LineNumbersMargin():
	
	def __init__( self, TextEditor ):
		self.TextEditor = TextEditor
		self.MarginArea = 20
		
	def Draw( self, context, widget, expose_event, width, height ):
		hidden_tag  = self.TextEditor.TextBuffer.get_tag_table().lookup("hidden")
		nr_linii    = self.TextEditor.TextBuffer.get_line_count()
		font_name   = widget.get_pango_context().get_font_description().get_family()
		font_size   = widget.get_pango_context().get_font_description().get_size() / pango.SCALE

		layout          = widget.create_pango_layout( str(nr_linii) )
		text_width, text_height = layout.get_pixel_size()
		self.MarginArea = text_width + 6
		self.TextEditor.SourceView.set_left_margin( self.MarginArea + 30 )
		#deseneaza fundalul
		self.TextEditor.SetCairoColorFromRgb( context, 193, 192, 215 )
		context.rectangle(0, 0, self.MarginArea, height)
		context.fill()
		#deseneaza textul
		#
		context.select_font_face ( font_name , cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
		context.set_font_size    ( float(font_size) )
		context.set_source_rgb(0, 0, 0)
		
		poz_y    = -self.TextEditor.GetVScrollBarValue() + font_size
		#
		for i in range( 0, nr_linii ):
			#daca linia curenta nu este ascunsa, arata line numberul
			iter = self.TextEditor.TextBuffer.get_iter_at_line(i)
			if not( iter.has_tag( hidden_tag ) ):
				context.move_to       ( 2, poz_y )
				context.show_text     ( str(i) )
				y, height = self.TextEditor.SourceView.get_line_yrange( iter )
				poz_y = poz_y + height
		#
		context.stroke ()

